let axios = require("axios");
let fs = require("fs");
const proxy = {
    host:"127.0.0.1",
    port: "7891",
    protocol: "http"
};
let headers = {
    accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7",
    cookie: "ipb_session_id=41682ef2211d381699a722378986d6e4; ipb_member_id=6722035; ipb_pass_hash=6f77e4c8018f782230ec66dc1c3f671c",
    referer: "https://e-hentai.org/",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "same-origin",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36"
}



// https://e-hentai.org/?f_search=bee
;
(async() => {

    await searchDoc({key: "Youkoso Jitsuryoku Shijou Shugi no Kyoushitsu e"})
    // exhibitionism    てるてるがーる   torture  軽井沢恵
})();


async function sleep(time) {
    return new Promise((a) => {
        setTimeout(() => {
            a()
        }, time);
    })
}


async function searchDoc(props) {
    if(!props.page) {
        props.page = 0;
    }
    if(!props.err) {
        props.err = 0;
    }
    let ret
    try {
        ret = await axios.get(encodeURI(`https://e-hentai.org/?page=${props.page}&f_search=${props.key}`), {
            headers,proxy
        })
    } catch (error) {
        props.err+=1
        console.log(`fetch ${props.page} error`);
        if(props.err < 5) {
            await sleep(4000 + Math.random() * 1000)
            await searchDoc(props)
        }
    }
    props.page++
    if(ret?.data) {
        fs.writeFileSync("./serch.html", ret.data)
        let matchs = ret.data.match(/<td class="gl2c"(?:(?!<\/td>)[\w\W])+<\/td>\s*<td class="gl3c glname"(?:(?!<\/td>)[\w\W])+/ig);
        if(matchs && matchs.length) {
            matchs = matchs.map((str, i) => {
                let ret = {}
                ret.title = /<div class="glink">((?:(?!<\/div>)[\w\W])+)<\/div>/ig.exec(str)[1]
                ret.gts = str.match(/<div class="gt" title="[^"]+">([^<]+)<\/div>/ig).map(v => /<div class="gt" title="[^"]+">([^<]+)<\/div>/ig.exec(v)[1])
                // 解析url
                if(/<a href="https:\/\/e-hentai.org\/g\/[^/]+\/[^/]+\/">/.test(str)) {
                    ret.url = /<a href="(https:\/\/e-hentai.org\/g\/[^/]+\/[^/]+\/)">/.exec(str)[1]
                }
                // 封面
                // <div class="glthumb" id="it2307944" style="top:-22px;height:421px">
				// 			<div><img style="height:375px;width:211px;top:-17px" alt="Careful What You Wish For 1"
				// 					title="Careful What You Wish For 1"
				// 					src="https://ehgt.org/t/e0/71/e071fa63d23a00371f7faef6e071d43677f63e86-342018-405-720-jpg_250.jpg" />
				// 			</div>
                if(/<div class="glthumb"[^>]+>\s*<div>\s*<img[^>]+src="([^"]+)" [^>]+\/?>/.test(str)) {
                    ret.image = RegExp.$1
                }
                // <div class="cn ct1" onclick="document.location='https://e-hentai.org/misc'">Misc
				// 					</div>   <div class="cn ct1" onclick="document.location='https://e-hentai.org/misc'">Misc</div>
                if(/<div class="cn ct.?"[^>]+>([^<]+)</.test(str)) {
                    ret.humb = RegExp.$1
                }
                // 页数
                // <div>11 pages</div>
                ret.pages = /<div>(\d+) pages<\/div>/.exec(str)[1]
                return ret
            })
            if(!fs.existsSync(`./searchs/${props.key}`)) {
                fs.mkdirSync(`./searchs/${props.key}`)
            }
            fs.writeFileSync(`./searchs/${props.key}/${props.page}.json`, JSON.stringify(matchs) )
            console.log("featch: " + props.page);
            await sleep(4000 + Math.random() * 1000)
            await searchDoc(props)
            return
        }
    }
    console.log(`fetch ${props.page} error`);
    if(props.err < 5) {
        props.page--
        await sleep(4000 + Math.random() * 1000)
        await searchDoc(props)
    }
}